//
//  LoginVC.swift
//  YunshuApp
//
//  Created by 周文杰 on 2017/5/10.
//  Copyright © 2017年 zwj. All rights reserved.
//

import UIKit
import ActiveLabel

class LoginVC: UIViewController {

    static let VCID = "LoginVC"
    
    @IBOutlet weak var phoneField: UITextField!
    
    @IBOutlet var pwdField: UITextField!
    
    @IBOutlet weak var protocolLabel2: UILabel!
    
    @IBOutlet weak var privacyLabe: UILabel!
    
    let PROTOCOL_STR1 = "点击“登录”，即表示同意"
    let PROTOCOL_STR2 = "《云数智惠用户协议》"
    let PROTOCOL_STR3 = "和"
    let PROTOCOL_STR4 = "《云数智惠隐私条款》"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "登录"
        self.view.isUserInteractionEnabled = true
        let attriTxt = NSMutableAttributedString.init(string: PROTOCOL_STR1, attributes: [NSAttributedString.Key.foregroundColor:Const.COLOR_GRAY])
        attriTxt.append(NSAttributedString.init(string: PROTOCOL_STR2, attributes: [NSAttributedString.Key.foregroundColor:UIColor.red]))
        protocolLabel2.attributedText = attriTxt
        
        self.view.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(LoginVC.agreement(_:))))
        
        let attriTxt2 = NSMutableAttributedString.init(string: PROTOCOL_STR3, attributes: [NSAttributedString.Key.foregroundColor:Const.COLOR_GRAY])
        attriTxt2.append(NSAttributedString.init(string: PROTOCOL_STR4, attributes: [NSAttributedString.Key.foregroundColor:UIColor.red]))
        privacyLabe.attributedText = attriTxt2
        
        
//        let label = protocolLabel()
//        label.numberOfLines = 0
//        label.enabledTypes = [.mention, .hashtag, .url]
//        label.text = PROTOCOL_STR1 + PROTOCOL_STR2 + PROTOCOL_STR3 + PROTOCOL_STR4
//        label.textColor = .black
//        let customType = ActiveType.custom(pattern: "\\s《.+》") //Regex that looks for "《》"
//        label.enabledTypes = [.mention, .hashtag, .url, customType]
//        label.text = "This is a post with #hashtags and a @userhandle."
//        label.customColor[customType] = UIColor.purple
//        label.customSelectedColor[customType] = UIColor.red
//
//        label.handleCustomTap(for: customType) { element in
//            print("Custom type tapped: \(element)")
//        }
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    @IBAction func onTapLogin(_ sender: Any) {
        
        let mobile1 = phoneField.text
        if mobile1?.count != 11 {
            UIUtility.alert("请输入正确的手机号");
            return;
        }
        let password = pwdField.text
        if (password?.count == 0){
            UIUtility.alert("请输入密码");
            return;
        }
        self.login(mobile1!,pwd : password!);
        
    }
    
    @IBAction func onTapRegister(_ sender: Any) {
        let vc = RegisterVC.init()
        self.navigationController?.pushViewController(vc)
    }
    
    @IBAction func onTapForgetPassword(_ sender: Any) {
        let vc = ChangePwdVC.init()
        self.navigationController?.pushViewController(vc)
    }
    
    
    //MARK:- 功能函数
    func login(_ phone:String , pwd:String) {
        
        let params = ["name":phone,"password":pwd]
        
        _ = NetworkManager.GET(NetworkManager.LOGIN,params:params) { (status, response) in
            if(status){
                
//                for dic in response.wtmList {
//                    let member = JSONDeserializer<XianhuoModel>.deserializeFrom(dict:dic as? NSDictionary)
//                    self.models.append(member!)
//                }
                User.loginWithResult(response.wtmDic);
                User.updateChannelId();
                
                DispatchQueue.main.async {
                    self.navigationController?.view.makeToast("登录成功！")
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }
        
    }
    
    @objc func agreement(_ ges:UITapGestureRecognizer) {
        
        let loc = ges.location(in: self.view)
        if protocolLabel2.frame.contains(loc) {
            let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
            vc.url = NetworkManager.BASE_URL + NetworkManager.AGREEMENT
            vc.title = "服务协议"
            self.navigationController?.pushViewController(vc)
        }else if privacyLabe.frame.contains(loc) {
            let vc:WebViewController = UIStoryboard.init(name: "mainStoryBoard", bundle: Bundle.main).instantiateViewController(withIdentifier: WebViewController.VCID) as! WebViewController
            vc.url = NetworkManager.BASE_URL + NetworkManager.PRIVACY
            vc.title = "隐私条款"
            self.navigationController?.pushViewController(vc)
        }else{
            phoneField.resignFirstResponder()
            pwdField.resignFirstResponder()
        }
    }
    
}
